<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
</head>
<body>
<div>
    用户名：<input type="text" id="username"/>
</div>
<div>
    密码：<input type="password" id="password"/>
</div>
<div>
    学号：  <input type="text" id="studentNo"/>
</div>
<div>
    姓名：  <input type="text" id="studentName"/>
</div>
<div>
    性别：  <select id="gender">
    <option value="0">男</option>
    <option value="1">女</option>
</select>
</div>
<div>
    邮箱：  <input type="text" id="email"/>
</div>
<div>
    <button id="register">注册</button>
</div>
<div>
    <button id="login"> 已注册，直接登录</button>
</div>

</body>

<script type="text/javascript">
    $(document).ready(function () {
        $("#login").click(function() {
            $(location).attr("href", "login.html")
        })
        $("#register").click(function() {
            var username = $("#username").val();
            var studentNo = $("#studentNo").val();
            var studentName = $("#studentName").val();
            var gender = $("#gender").val();
            var email = $("#email").val();
            var password = $("#password").val();

            $.ajax({
                type: 'post',
                url: 'http://localhost:8090/user/register',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify({
                    "username": username,
                    "studentNo": studentNo,
                    "studentName": studentName,
                    "gender": gender,
                    "email": email,
                    "password": password
                }),
                success: function(data) {
                    console.log(data)
                    if (data.code == 200) {
                        alert("注册成功")
                        $(location).attr("href", "login.html")
                    } else if (data.code == 500) {
                        alert("该学号已被注册，请直接登录");
                    } else {
                        alert(data.message);
                    }
                },
                error: function() {
                    alert("注册失败");
                }
            });
        });
    });
</script>

</html>